package com.i360day.invoker.http;

import com.i360day.invoker.BodyTemplate;
import com.i360day.invoker.RequestTemplate;

import java.net.URI;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

/**
 * @author liju.z
 */
public class Request {

    private final URI uri;
    private final Map<String, Collection<String>> headers;
    private final BodyTemplate body;
    private final RequestTemplate requestTemplate;

    public Request(URI uri, Map<String, Collection<String>> headers, BodyTemplate body, RequestTemplate requestTemplate) {
        this.uri = uri;
        this.headers = headers;
        this.body = body;
        this.requestTemplate = requestTemplate;
    }

    public static Request create(URI uri, Map<String, Collection<String>> headers, BodyTemplate body, Charset charset, RequestTemplate requestTemplate) {
        return new Request(uri, headers, body, requestTemplate);
    }

    public URI getUri() {
        return uri;
    }

    public Map<String, Collection<String>> headers() {
        return Collections.unmodifiableMap(headers);
    }

    public BodyTemplate body() {
        return body;
    }

    public RequestTemplate requestTemplate() {
        return requestTemplate;
    }


    public static Request of(URI uri, RequestTemplate requestTemplate){
        return new Request(uri, requestTemplate.getHeaders(), requestTemplate.getBodyTemplate(), requestTemplate);
    }
    /**
     * Charset of the request.
     *
     * @return the current character set for the request, may be {@literal null} for binary data.
     */
    public Charset charset() {
        return Charset.forName("UTF-8");
    }


    public Request replaceUri(URI newURI) {
        return new Request(newURI, headers(), body(), requestTemplate());
    }

    @Override
    public Request clone(){
        return Request.create(getUri(), headers(), body(), charset(), requestTemplate());
    }
}
